package cn.sofwin.sb;


import cn.sofwin.sb.core.annotation.MyDao;
import cn.sofwin.sb.dao.UserDao;
import cn.sofwin.sb.service.UserService;

import java.lang.reflect.Field;

public class Main {
    public static void main(String[] args) {

        UserService userService = new UserService();
        initAutowired(userService);
        System.out.println("UserDao"+userService.getUserDao());

    }
    public static  void initAutowired(Object obj){
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for(Field field:fields){
                MyDao myDao = field.getAnnotation(MyDao.class);
                if(myDao!=null){
                    System.out.println("发现友军"+field.getName());
                    //第一阶段  实现设置属性值的操作
                    field.setAccessible(true);
                    //field.set(obj,new UserDao());

                    //第二阶段，动态的设置对应数据类型的对象
                    System.out.println(field.getType());
                    field.set(obj,field.getType().newInstance());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
